package com.shujia.wyh.day09;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo5 {
    public static void main(String[] args) {
        //1、创建集合对象
        Collection c1 = new ArrayList();

        //2、创建学生元素对象
        Student s1 = new Student("孙悟空", 500);
        Student s2 = new Student("沙悟净", 600);
        Student s3 = new Student("猪八戒", 400);
        Student s4 = new Student("唐僧", 800);

        //3、将学生对象添加到集合中
        c1.add(s1);
        c1.add(s2);
        c1.add(s3);
        c1.add(s4);

        //4、遍历集合
        //方式1：先转数组再遍历
//        Object[] array = c1.toArray();
//        for(int i=0;i<array.length;i++){
//            System.out.println(array[i]);
//        }
        //方式2：迭代器遍历
        Iterator iterator = c1.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
